package com.jiehoo.utils.io.filter;

import java.io.*;

import com.jiehoo.utils.StringUtil;

public class FileTypeFilter extends CombineFileFilter {
	String[] suffixList;
	String description;
	boolean caseSensitive = false;

	public FileTypeFilter(String suffix) {
		this(suffix, SWING);
	}

	public FileTypeFilter(String suffix, int type) {
		super(type);
		suffixList = new String[1];
		suffixList[0] = suffix;
	}

	public FileTypeFilter(String[] suffixList) {
		this(suffixList, SWING);
	}

	public FileTypeFilter(String[] suffixList, int type) {
		super(type);
		this.suffixList = suffixList;
	}

	public FileTypeFilter(String[] suffixList, String description) {
		super(SWING);
		this.suffixList = suffixList;
		this.description = description;
	}

	public FileTypeFilter(String[] suffixList, boolean caseSensitive) {
		this(suffixList, caseSensitive, SWING);
	}

	public FileTypeFilter(String[] suffixList, boolean caseSensitive, int type) {
		super(type);
		this.suffixList = suffixList;
		this.caseSensitive = caseSensitive;
	}

	public FileTypeFilter(String[] suffixList, boolean caseSensitive,
			String description) {
		super(SWING);
		this.suffixList = suffixList;
		this.caseSensitive = caseSensitive;
		this.description = description;
	}

	public void setCaseSensitive(boolean caseSensitive) {
		this.caseSensitive = caseSensitive;
	}

	public boolean getCaseSensitive() {
		return caseSensitive;
	}

	protected boolean acceptFile(File file) {
		String filename = file.getName();
		int dot = filename.lastIndexOf(".");
		if (dot == -1) {
			return false;
		}
		return StringUtil.contains(suffixList, filename.substring(dot + 1),
				caseSensitive);
	}

	public String getDescription() {
		if (description != null) {
			return description;
		}
		return StringUtil.combineStringArray(suffixList, "/");
	}

}
